// package com.zmn.oms.task.es.business.impl.orderpart;
//
// import com.alibaba.fastjson.JSON;
// import com.google.common.collect.Lists;
// import com.zmn.common.utils.collection.CollectionUtil;
// import com.zmn.common.utils.reflect.BeanMapper;
// import com.zmn.common.utils.string.StringUtil;
// import com.zmn.manager.mq.interfaces.ZmnMQSender;
// import com.zmn.oms.common.utils.MathUtil;
// import com.zmn.oms.model.entity.part.OrderPart;
// import com.zmn.oms.model.entity.product.OrderProduct;
// import com.zmn.oms.model.entity.work.OrderWork;
// import com.zmn.oms.model.es.EsOrderPart;
// import com.zmn.oms.model.vo.order.EsOrderDetailVO;
// import com.zmn.oms.services.interfaces.detail.OrderDetailService;
// import com.zmn.oms.services.interfaces.part.EsOrderPartService;
// import com.zmn.oms.services.interfaces.part.OrderPartService;
// import com.zmn.oms.services.interfaces.product.OrderProductService;
// import com.zmn.oms.services.interfaces.work.OrderWorkService;
// import com.zmn.oms.task.es.business.interfaces.orderpart.SyncEsOrderPartBService;
// import org.elasticsearch.index.query.QueryBuilders;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
// import org.springframework.data.elasticsearch.core.query.DeleteQuery;
// import org.springframework.stereotype.Service;
//
// import java.util.HashMap;
// import java.util.List;
// import java.util.Map;
// import java.util.stream.Collectors;
//
// /**
//  * 类描述：同步订单实现
//  *
//  * @author lujia
//  * @date 2018/11/23 19:38
//  */
// @Service
// public class SyncEsOrderPartBServiceImpl implements SyncEsOrderPartBService {
//
//     private static final Logger logger = LoggerFactory.getLogger(SyncEsOrderPartBServiceImpl.class);
//
//     @Autowired(required = false)
//     private OrderPartService orderPartService;
//     @Autowired(required = false)
//     private OrderWorkService orderWorkService;
//     @Autowired(required = false)
//     private OrderDetailService orderDetailService;
//     @Autowired(required = false)
//     private EsOrderPartService esOrderPartService;
//     @Autowired(required = false)
//     private ElasticsearchTemplate elasticsearchTemplate;
//     @Autowired(required = false)
//     private ZmnMQSender zmnMQSender;
//     @Autowired
//     private OrderProductService orderProductService;
//
//     @Override
//     public void sync(List<Long> orderIdList) {
//
//         logger.debug("#orderpartchange#sync start orderid list : {}", JSON.toJSONString(orderIdList));
//
//         if (orderIdList == null || orderIdList.isEmpty()) {
//             return;
//         }
//
//         // 初始化map值 = 工单数量 * 5
//         int count = orderIdList.size() * 5;
//
//         // order part
//         Map<Long, List<EsOrderPart>> esOrderPartMap = new HashMap(count);
//
//         // 工单配件
//         List<OrderPart> orderPartList = orderPartService.listEsOrderPartByOrderIdList(orderIdList);
//
//         if (orderPartList.isEmpty()) {
//             logger.debug("#orderpartchange# 工单没有配件");
//             return;
//         }
//
//         // 1. 组装订单配件扩展数据
//         processOrderPartList(orderPartList, esOrderPartMap);
//
//         // 工单list
//         List<OrderWork> orderWorkList = orderWorkService.listEsOrderWorkByOrderIdList(orderIdList);
//         if (orderWorkList.isEmpty()) {
//             logger.error("#orderpartchange# sync fail, not found orderid list : {}", JSON.toJSONString(orderIdList));
//             return;
//         }
//         Map<Long, OrderWork> orderWorkMap = orderWorkList.stream()
//                 .collect(Collectors.toMap(OrderWork::getWorkId, orderWork -> orderWork));
//         // 2.组装工单信息
//         processOrderWorkList(orderWorkMap, esOrderPartMap);
//
//         // 工单详情list
//         List<EsOrderDetailVO> orderDetailList = orderDetailService.listEsOrderDetailByOrderIdList(orderIdList);
//         if (orderDetailList.isEmpty()) {
//             logger.error("#orderpartchange# sync fail, not found orderid list : {}", JSON.toJSONString(orderIdList));
//             return;
//         }
//         Map<Long, EsOrderDetailVO> orderDetailMap = orderDetailList.stream()
//                 .collect(Collectors.toMap(EsOrderDetailVO::getDetailId, orderDetail -> orderDetail));
//         // 2.组装工单信息
//         processOrderDetailList(orderDetailMap, esOrderPartMap);
//
//         // 5.组装工单产品列表
//         // 产品
//         List<OrderProduct> orderProductList = orderProductService.listOrderProductByOrderIdList(orderIdList);
//         Map<Long, List<OrderProduct>> orderProductGroupMap = orderProductList.stream().collect(Collectors.groupingBy(OrderProduct::getOrderId));
//         processOrderShowProductName(orderProductGroupMap, esOrderPartMap);
//
//         // 如果为 1 代表单条删除
//         if (orderIdList.size() == 1) {
//             orderIdList.forEach(orderId -> {
//                 DeleteQuery deleteQuery = new DeleteQuery();
//                 deleteQuery.setQuery(QueryBuilders.termQuery("orderId", orderId));
//                 elasticsearchTemplate.delete(deleteQuery, EsOrderPart.class);
//             });
//         }
//
//         // 保存
//         List<EsOrderPart> esOrderPartList = Lists.newArrayListWithCapacity(count);
//         esOrderPartMap.values().forEach(list -> {
//             esOrderPartList.addAll(list);
//         });
//         esOrderPartService.saveAll(esOrderPartList);
//         logger.debug("#orderpartchange# sync end orderid list : {}", JSON.toJSONString(orderIdList));
//     }
//
//     /**
//      * 组装工单详情
//      * @param orderDetailMap
//      * @param esOrderPartMap
//      */
//     private void processOrderDetailList(Map<Long, EsOrderDetailVO> orderDetailMap,
//             Map<Long, List<EsOrderPart>> esOrderPartMap) {
//         orderDetailMap.forEach((key, value) -> {
//             List<EsOrderPart> esOrderPartList = esOrderPartMap.get(key);
//             if (esOrderPartList == null) {
//                 return;
//             }
//             esOrderPartList.forEach(esOrderPart -> {
//                 esOrderPart.setProvinceId(value.getProvinceId());
//                 esOrderPart.setCountyId(value.getCountyId());
//                 esOrderPart.setServCategName(value.getServCategName());
//                 esOrderPart.setCityName(value.getCityName());
//                 esOrderPart.setProductInfo(value.getProductInfo());
//             });
//         });
//     }
//
//     /**
//      * 组装工单数据
//      * @param orderWorkMap
//      * @param esOrderPartMap
//      */
//     private void processOrderWorkList(Map<Long, OrderWork> orderWorkMap,
//             Map<Long, List<EsOrderPart>> esOrderPartMap) {
//         orderWorkMap.forEach((key, value) -> {
//             List<EsOrderPart> esOrderPartList = esOrderPartMap.get(key);
//             if (esOrderPartList == null) {
//                 return;
//             }
//             esOrderPartList.forEach(esOrderPart -> {
//
//                 esOrderPart.setPlat(value.getPlat());
//                 esOrderPart.setPlatWork(value.getPlatWork());
//
//                 esOrderPart.setOrgOneId(value.getOrgOneId());
//                 esOrderPart.setOrgTwoId(value.getOrgTwoId());
//                 esOrderPart.setOrgThreeId(value.getOrgThreeId());
//                 esOrderPart.setOrgFourId(value.getOrgFourId());
//                 esOrderPart.setChannelOneId(value.getChannelOneId());
//                 esOrderPart.setChannelTwoId(value.getChannelTwoId());
//
//                 esOrderPart.setCompanyId(value.getCompanyId());
//                 esOrderPart.setCompanyName(value.getCompanyName());
//                 esOrderPart.setManageCompanyId(value.getManageCompanyId());
//
//                 esOrderPart.setCityId(value.getCityId());
//                 esOrderPart.setChannelId(value.getChannelId());
//                 esOrderPart.setChannelName(value.getChannelName());
//                 esOrderPart.setChannelTypeId(value.getChannelTypeId());
//                 esOrderPart.setServCategId(value.getServCategId());
//
//                 esOrderPart.setCodeEnd6(StringUtil.cutEnd(value.getWorkId().toString(), 6));
//                 esOrderPart.setStatus(value.getStatus());
//                 esOrderPart.setResultStatus(value.getResultStatus());
//                 esOrderPart.setReimburseStatus(value.getPartReimburseStatus());
//                 esOrderPart.setReimburseTime(value.getPartReimburseTime());
//                 esOrderPart.setAccountTime(value.getAccountTime());
//
//                 esOrderPart.setMasterName(value.getMasterName());
//                 esOrderPart.setMasterId(value.getMasterId());
//
//                 esOrderPart.setBizLine(value.getBizLine());
//                 esOrderPart.setManageCompanyName(value.getManageCompanyName());
//                 esOrderPart.setTotalAmount(value.getTotalAmount());
//             });
//         });
//     }
//
//     private void processOrderShowProductName(Map<Long, List<OrderProduct>> orderProductGroupMap, Map<Long, List<EsOrderPart>> esOrderPartMap) {
//         orderProductGroupMap.forEach((key,value)->{
//             List<EsOrderPart> esOrderPartList = esOrderPartMap.get(key);
//             if (esOrderPartList == null) {
//                 return;
//             }
//             if (CollectionUtil.isNullOrEmpty(value)) {
//                 return;
//             }
//             String showProductName = value.get(0).getShowProductName();
//             esOrderPartList.forEach(esOrderPart -> {
//                 esOrderPart.setShowProductName(showProductName);
//             });
//         });
//     }
//
//     /**
//      * 组装工单配件数据
//      * @param orderPartList
//      * @param esOrderPartMap
//      */
//     private void processOrderPartList(List<OrderPart> orderPartList,
//             Map<Long, List<EsOrderPart>> esOrderPartMap) {
//
//         orderPartList.forEach(part -> {
//             Long orderId = part.getOrderId();
//             List<EsOrderPart> esOrderPartList;
//
//             EsOrderPart esOrderPart = new EsOrderPart();
//             BeanMapper.copy(part, esOrderPart);
//             esOrderPart.setReimburseStatus(part.getReimburse());
//
//             if (part.getPrice() != null && part.getNumber() != null) {
//                 esOrderPart.setAmount(MathUtil.amountMultiplyNumber(part.getPrice(), part.getNumber()));
//             }
//
//             if (esOrderPartMap.containsKey(orderId)) {
//                 esOrderPartList = esOrderPartMap.get(orderId);
//             } else {
//                 esOrderPartList = Lists.newArrayList();
//                 esOrderPartMap.put(part.getOrderId(), esOrderPartList);
//             }
//             esOrderPartList.add(esOrderPart);
//         });
//     }
// }